/*
 */

package com.railway.news.dao.impl;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;
import cn.org.rapid_framework.page.Page;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.railway.common.base.BaseIbatisDao;
import com.railway.common.base.IdGenerator;
import com.railway.news.dao.ICategoryDao;
import com.railway.news.model.Category;
import com.railway.news.model.CategoryUserLink;
import com.railway.news.vo.query.CategoryQuery;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */


@Repository("categoryDao")
public class CategoryDaoImpl extends BaseIbatisDao<Category,java.lang.String> implements ICategoryDao{
	
	@Override
	public String getIbatisSqlMapNamespace() {
		return "Category";
	}
	
	public Category getById(java.lang.String id){
		return super.getById( id);
	}
	
	public void deleteById(java.lang.String id){
		super.deleteById(id);
	}
	
	/** 插入数据 */
	public void save(Category entity){
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setId(IdGenerator.genUUIDStr());
		}
		super.save(entity);
	}
	
	/** 更新数据 */
	public void update(Category entity){
		super.update(entity);
	}

	public boolean isUnique(Category entity, String uniquePropertyNames){
		return super.isUnique( entity, uniquePropertyNames);
	}
	
	public List<Category> findAll(){
	List list = null;
	
	list = this.getSqlMapClientTemplate().queryForList("Category.findAll");
	return list;
	}
	
	public void saveOrUpdate(Category entity) {
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setId(IdGenerator.genUUIDStr());
			super.save(entity);
		}
		else 
			super.update(entity);
	}
	
	public Page findPage(CategoryQuery query) {
		return pageQuery("Category.findPage",query);
	}


	public void saveCategoryUserLinkBatch(final List varList) {
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { 
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 
                executor.startBatch(); 
                for (int i = 0; i < varList.size(); i++) { 
                	CategoryUserLink evDto = (CategoryUserLink) varList.get(i); 
                    executor.insert("Category.insert_category_user_link", evDto); 
                } 
                executor.executeBatch(); 
                return null; 
            } 
        }); 
		
	}
	public void saveCategoryUserLink(CategoryUserLink entity) {
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setId(IdGenerator.genUUIDStr());
		 }
		getSqlMapClientTemplate().insert("Category.insert_category_user_link", entity);  
	}
	public List getPermitCategoriesByUser(CategoryUserLink culink) {
		try{

		return getSqlMapClientTemplate().queryForList("Category.getCategoryUserLinkById",culink);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

 
	public List<Category> findByType(String type) {
		
		List list = null;		
		try{
		Map map = new HashMap();
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("Category.findByType",map);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
		
	}


	public void removeLinkByUserId(String userId) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().delete("Category.delete_Category_User_Link_ByUSERID", userId);
	}

	
	public void deleteCategoryUserLinkByCatId(String catid) {
		Map map = new HashMap();
		map.put("catid", catid);
		getSqlMapClientTemplate().delete("Category.deleteCategoryUserLinkByCatId", map);
		
	}
	public void deleteCategoryUserLinkById(String id) {
		Map map = new HashMap();
		map.put("id", id);
		getSqlMapClientTemplate().delete("Category.delete_Category_User_Link_ByID", map);
		
	}

	public List getUsernamesByMap(Map map) {
		List list = null;		
		
		try{
 
		list = this.getSqlMapClientTemplate().queryForList("Category.getUsernamesByMap",map);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
}
